package com.example.demo.utils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Map.Entry;
public class SendPostUtil {
    /**
     * 向指定 URL 发送POST方法的请求
     */
    public static String sendPostUTF8(String url,  Map<String,String> params) throws UnsupportedEncodingException, IOException {
        return sendPostUTF8(url, params, null);
    }
    public static String sendPostUTF8(String url,Map<String,String> params, Map<String, String> header) throws UnsupportedEncodingException, IOException {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        StringBuilder stringBuilder = new StringBuilder();
        //拼接参数
        for (Map.Entry<String, String> param : params.entrySet()){
            stringBuilder.append(param.getKey());
            stringBuilder.append("=");
            stringBuilder.append(URLEncoder.encode(param.getValue(),"UTF-8"));
            stringBuilder.append("&");
        }
        stringBuilder.deleteCharAt(stringBuilder.lastIndexOf("&"));
        System.err.println("参数:"+stringBuilder.toString());
        URL realUrl = new URL(url);
        // 打开和URL之间的连接
        URLConnection conn = realUrl.openConnection();
        //设置超时时间
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(15000);
        // 设置通用的请求属性
        if (header!=null) {
            for (Entry<String, String> entry : header.entrySet()) {
                conn.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 发送POST请求必须设置如下两行
        conn.setDoOutput(true);
        conn.setDoInput(true);
        // 获取URLConnection对象对应的输出流
        out = new PrintWriter(conn.getOutputStream());
        // 发送请求参数
        out.print(stringBuilder.toString());
        // flush输出流的缓冲
        out.flush();
        // 定义BufferedReader输入流来读取URL的响应
        in = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), "UTF-8"));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        if(out!=null){
            out.close();
        }
        if(in!=null){
            in.close();
        }
        return result;
    }
    public static String sendPostGBK(String url,Map<String,String> params, Map<String, String> header) throws UnsupportedEncodingException, IOException {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        StringBuilder stringBuilder = new StringBuilder();
        //拼接参数
        for (Map.Entry<String, String> param : params.entrySet()){
            stringBuilder.append(param.getKey());
            stringBuilder.append("=");
            if("text".equals(param.getKey())){
                stringBuilder.append(URLEncoder.encode(param.getValue(),"GBK"));
            }
            else{
                stringBuilder.append(URLEncoder.encode(param.getValue(),"UTF-8"));
            }
            stringBuilder.append("&");
        }
        stringBuilder.deleteCharAt(stringBuilder.lastIndexOf("&"));
        System.out.println("参数:"+stringBuilder.toString());
        URL realUrl = new URL(url);
        // 打开和URL之间的连接
        URLConnection conn = realUrl.openConnection();
        //设置超时时间
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(15000);
        // 设置通用的请求属性
        if (header!=null) {
            for (Entry<String, String> entry : header.entrySet()) {
                conn.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 发送POST请求必须设置如下两行
        conn.setDoOutput(true);
        conn.setDoInput(true);
        // 获取URLConnection对象对应的输出流
        out = new PrintWriter(conn.getOutputStream());
        // 发送请求参数
        out.print(stringBuilder.toString());
        // flush输出流的缓冲
        out.flush();
        // 定义BufferedReader输入流来读取URL的响应
        in = new BufferedReader(
                new InputStreamReader(conn.getInputStream(), "GBK"));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        if(out!=null){
            out.close();
        }
        if(in!=null){
            in.close();
        }
        return result;
    }
}

